from common.setting import ensure_path_sep
from third_services.aliexpress.api.api import AliexpressApi
from utils.mysqlUtils.db_pool import Database


class PublishLimit:

    test_zhcxkj_center_listing_db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")

    def __init__(self, source_id):
        self.source_id = source_id

    def get_business_type(self):
        type_sql = '''
        SELECT business_type FROM `test_zhcxkj_center_listing_comprehensive`.`t_aliexpress_source_account_info` 
        where  source_id = %s
        '''
        business_type_data = self.test_zhcxkj_center_listing_db.fetch_all(type_sql, (self.source_id,))
        business_type = set(map(lambda x: int(x["business_type"]),business_type_data))
        business_type_list = list(business_type)
        return business_type_list

    def get_choice_limit(self):
        channel_seller_id = '''
        SELECT account_seller_id FROM `test_zhcxkj_center_listing_comprehensive`.`t_aliexpress_source_account_info` 
        where  source_id = %s and  business_type = 2
        '''
        channel_seller_data = self.test_zhcxkj_center_listing_db.fetch_all(channel_seller_id,(self.source_id,))
        channel_seller_id = int(channel_seller_data[0]["account_seller_id"])
        status_1 = "ONLINE"
        online_list_data = AliexpressApi(self.source_id).get_choice_product_list(channel_seller_id, 1, status_1)
        online_number = int(online_list_data["result"]["total_count"])
        status_2 = "PENDING_APPROVAL"
        auth_list_data = AliexpressApi(self.source_id).get_choice_product_list(channel_seller_id, 1, status_2)
        auth_number = int(auth_list_data["result"]["total_count"])
        source_limit = {"店铺id":self.source_id,"全托在线":online_number, "全托审核中":auth_number,
                        "总额度": online_number+auth_number}
        return source_limit

    def limit_verification(self):
        rst = []
        # 实现pop商品的 刊登额度统计
        limit_data = AliexpressApi(self.source_id).getMerchantProfileGet()
        source_profile = limit_data["data"]["aliexpress_merchant_profile_get_response"]["profile"]
        online_number = int(source_profile["online_store_product_number"])
        auth_number = int(source_profile["auth_store_product_number"])

        business_type = self.get_business_type()

        if len(business_type) == 1:
            if business_type[0] == 1:
                pop_source_limit = {"店铺id":self.source_id,"pop在线": online_number, "pop审核中": auth_number,
                                    "总额度": online_number+auth_number}
                rst.append(pop_source_limit)
            elif business_type[0] == 2:
                choice_source_limit = {"店铺id":self.source_id,"全托在线": online_number, "全托审核中": auth_number,
                                       "总额度": online_number+auth_number}
                rst.append(choice_source_limit)
        elif len(business_type) == 2:
            pop_source_limit = {"店铺id":self.source_id,"pop在线": online_number, "pop审核中": auth_number,
                                "总额度": online_number+auth_number}
            rst.append(pop_source_limit)
            # 实现全托商品的 刊登额度统计
            choice_source_limit = self.get_choice_limit()
            rst.append(choice_source_limit)
        return rst


if __name__ == '__main__':
    # PublishLimit(20463).get_choice_limit()
    print(PublishLimit(22077).limit_verification())